package cloudspace.util.jassistwrappers;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * The Class VMURL. This is a set of wrapper functions for use when localizing
 * and globalizing URLs in javassist.
 */
public class VMURL
{

    /**
     * Globalize a url.
     * 
     * @param url
     *            the url to globalize
     * 
     * @return the URL (System View)
     */
    public static URL globalizeURL( URL url )
    {
        if ( url == null )
        {
            return url;
        }
        try
        {
            if ( url.getProtocol().equals( "file" ) )
            {
                String scheme = url.getProtocol();
                String host = url.getHost();
                String path = url.getPath();
                path = VMFile.toGlobalName( path ) + url.getFile();

                return new URL( scheme, host, 0, path );

            }
        }
        catch ( MalformedURLException e )
        {
            ;
        }
        return url;

    }


    /**
     * Localize the file URL
     * 
     * @param url
     *            the url to localize
     * 
     * @return the localized URL (User View)
     */
    public static URL localizeFileURL( File myFile, URL url )
    {
        try
        {
            if ( url.getProtocol().equals( "file" ) )
            {
                String scheme = url.getProtocol();
                String host = url.getHost();
                String path = myFile.getPath();
                int port = url.getPort();
                path = VMFile.toLocalAbsName( myFile, path );

                return new URL( scheme, host, port, path );

            }
        }
        catch ( MalformedURLException e )
        {
            ;
        }
        return url;
    }
}
